package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.swing.plaf.basic.BasicOptionPaneUI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * @Author: liuaojun
 * @CreateTime: 2025-02-21
 * @Description:
 * @Version: 1.0
 */

@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo> implements ApartmentInfoService {



    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    private ApartmentFacilityService apartmentFacilityService;

    @Autowired
    private ApartmentLabelService apartmentLabelService;

    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;

    @Autowired
    ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    GraphInfoMapper graphInfoMapper;

    @Autowired
    LabelInfoMapper labelInfoMapper;

    @Autowired
    FacilityInfoMapper facilityInfoMapper;

    @Autowired
    FeeValueMapper feeValueMapper;





    /**
    * @Author:liuaojun
    * @Date:2025-02-21 18:20
    * @Description:保存或者更新公寓信息，思路：先删除再添加数据
    */
    @Override
    public void saveApartmentInfo(ApartmentSubmitVo apartmentSubmitVo) {
        //更新或者添加主表数据
        this.saveOrUpdate(apartmentSubmitVo);

        //判断是更新还是添加，id为空新增，id不为空更新（通过id修改）
        if(apartmentSubmitVo.getId()!=null){

            //删除图片信息
            LambdaQueryWrapper<GraphInfo> graphWrapper = new LambdaQueryWrapper<>();
            graphWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT).eq(GraphInfo::getItemId, apartmentSubmitVo.getId());
            graphInfoService.remove(graphWrapper);

            //删除公寓配套信息get
            LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getId, apartmentSubmitVo.getId()).eq(ApartmentFacility::getApartmentId,ItemType.APARTMENT);
            apartmentFacilityService.remove(apartmentFacilityLambdaQueryWrapper);

            //删除公寓标签信息
            LambdaQueryWrapper<ApartmentLabel> apartmentLabelWrapper = new LambdaQueryWrapper<>();
            apartmentLabelWrapper.eq(ApartmentLabel::getId, apartmentSubmitVo.getId()).eq(ApartmentLabel::getApartmentId,ItemType.APARTMENT);
            apartmentLabelService.remove(apartmentLabelWrapper);

            //删除公寓杂费信息
            LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueWrapper = new LambdaQueryWrapper<>();
            apartmentFeeValueWrapper.eq(ApartmentFeeValue::getApartmentId,apartmentSubmitVo.getId());
            apartmentFeeValueService.remove(apartmentFeeValueWrapper);


        }
        //修改图片信息
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if(!CollectionUtils.isEmpty(graphVoList)){
            ArrayList<GraphInfo> graphInfos = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfo.setItemId(apartmentSubmitVo.getId());
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfos.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfos);
        }


        //修改公寓配套信息
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIds)){
            List<ApartmentFacility> apartmentFacilities = new ArrayList<>();
            for (Long facilityInfoId : facilityInfoIds) {
                ApartmentFacility build = ApartmentFacility.builder().apartmentId(apartmentSubmitVo.getId()).facilityId(facilityInfoId).build();
                apartmentFacilities.add(build);
            }
            apartmentFacilityService.saveBatch(apartmentFacilities);
        }


        //修改公寓标签信息
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        if (!CollectionUtils.isEmpty(labelIds)){
            ArrayList<ApartmentLabel> apartmentLabels = new ArrayList<>();
            for (Long labelId : labelIds) {
                ApartmentLabel build = ApartmentLabel.builder().apartmentId(apartmentSubmitVo.getId()).labelId(labelId).build();
                apartmentLabels.add(build);
            }
            apartmentLabelService.saveBatch(apartmentLabels);
        }

        //修改公寓杂费信息
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        if(!CollectionUtils.isEmpty(feeValueIds)){
            ArrayList<ApartmentFeeValue> apartmentFeeValues = new ArrayList<>();
            for (Long feeValueId : feeValueIds) {
                ApartmentFeeValue build = ApartmentFeeValue.builder().apartmentId(apartmentSubmitVo.getId()).feeValueId(feeValueId).build();
                apartmentFeeValues.add(build);
            }
            apartmentFeeValueService.saveBatch(apartmentFeeValues);
        }

    }

    @Override
    public IPage<ApartmentItemVo> pageItem(IPage<ApartmentItemVo> page, ApartmentQueryVo apartmentQueryVo) {
        return apartmentInfoMapper.pageApartmentItemByQuery(page, apartmentQueryVo);
    }

    @Override
    public ApartmentDetailVo getApartmentDetailById(Long id) {
        //查询apartmentInfo信息
        ApartmentInfo apartmentInfo = this.getById(id);
        if(apartmentInfo == null){
            return null;
        }
        //查询GraphInfo
        List<GraphVo> list=graphInfoMapper.selectListByItemTypeAndId(ItemType.APARTMENT,id);
        //查询LabelInfo
        List<LabelInfo> lablelsit=labelInfoMapper.selectListByApartmentId(id);

        //查询FacilityInfo
        List<FacilityInfo> facilityList= facilityInfoMapper.selectListByApartmentId(id);
        //查询FeeValue
        List<FeeValueVo> feeValueVoList=feeValueMapper.selectListByApartmentId(id);
        //返回给ApartmentDetailVo
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo);
        apartmentDetailVo.setGraphVoList(list);
        apartmentDetailVo.setFacilityInfoList(facilityList);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);
        apartmentDetailVo.setLabelInfoList(lablelsit);
        return apartmentDetailVo;
    }

}
